using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using Extensions;
using Loading.Controllers;

namespace Loading.Models {
    public class CartrigeLotRepository : ICartrigeLotRepository {
        LoadingContext context = new LoadingContext();

        public IQueryable<CartrigeLot> All {
            get { return context.CartrigeLots; }
        }

        public IQueryable<CartrigeLot> AllIncluding(params Expression<Func<CartrigeLot, object>>[] includeProperties) {
            IQueryable<CartrigeLot> query = context.CartrigeLots;
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query;
        }


        public CartrigeLot Find(int id)
        {
            DateTime d;
            
            return context.CartrigeLots.Find(id);

        }

        public IEnumerable<CartrigeLot> Find(Expression<Func<CartrigeLot, bool>> predicate) {
            return context.CartrigeLots.Where(predicate);

        }



        public IEnumerable<CartrigeLot> FindByNameAndRecipieName(string Name, string recipieName)
        {
            var list = context.CartrigeLots;
            return (from cartrigeLot in list from note in cartrigeLot.Notes where cartrigeLot.Name == Name && note.IsFieldNote select cartrigeLot).ToList();
        }

       

        public void InsertOrUpdate(CartrigeLot cartrigelot) {
            if (cartrigelot.CartrigeLotId == default(int)) {
                // New entity
                context.CartrigeLots.Add(cartrigelot);
            }
            else {
                // Existing entity
                context.Entry(cartrigelot).State = EntityState.Modified;
            }
        }

        public void Delete(int id) {
            var cartrigelot = context.CartrigeLots.Find(id);
            context.CartrigeLots.Remove(cartrigelot);
        }

        public void Save() {
            context.SaveChanges();
        }
    }

    public interface ICartrigeLotRepository {
        IQueryable<CartrigeLot> All { get; }
        IQueryable<CartrigeLot> AllIncluding(params Expression<Func<CartrigeLot, object>>[] includeProperties);
        CartrigeLot Find(int id);
        void InsertOrUpdate(CartrigeLot cartrigelot);
        void Delete(int id);
        void Save();
        IEnumerable<CartrigeLot> Find(Expression<Func<CartrigeLot, bool>> predicate);
    }
}